package com.yuexiao.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuexiao.config.RedisUtil;
import com.yuexiao.pojo.AddressBook;
import com.yuexiao.service.AddressBookService;
import com.yuexiao.mapper.AddressBookMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【address_book(地址管理)】的数据库操作Service实现
 * @createDate 2022-08-10 14:10:57
 */
@Component
@Service(interfaceClass = AddressBookService.class)
@Transactional(rollbackFor = Exception.class)
public class AddressBookServiceImpl extends ServiceImpl<AddressBookMapper, AddressBook>
        implements AddressBookService {

    @Autowired
    private RedisUtil redisUtil;


    /**
     * 根据用户id查询该用户的地址列表
     *
     * @param userId Long 用户主键
     * @return List<AddressBook> 地址列表
     */
    @Override
    public List<AddressBook> getAddressBookByUserId(Long userId) {
        return baseMapper.selectList(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId, userId));
    }


    /**
     * 插如新的地址
     *
     * @param addressBook
     * @return boolean true or false
     */
    @Override
    public boolean insertAddressBook(AddressBook addressBook) {
        return baseMapper.insert(addressBook) > 0 ? true : false;
    }


    /**
     * 根据地址主键更新默认状态
     *
     * @param id Long 地址主键id
     * @return boolean true or false
     */
    @Override
    public boolean updateDefaultById(Long id) {
        //从redis中获取登录用户id
        Object loginId = redisUtil.get("loginId");
        if (loginId == null) {
            throw new RuntimeException("未登录");
        }
        //将该用户所有的地址状态先置为0
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId,loginId);
        updateWrapper.set(AddressBook::getIsDefault,0);
        baseMapper.update(null,updateWrapper);

        //再根据id修改状态
        AddressBook addressBook = new AddressBook();
        addressBook.setId(id);
        addressBook.setIsDefault(1);
        baseMapper.updateById(addressBook);
        return true;
    }


    /**
     * 根据地址主键查询地址数据
     * @param id Long 地址主键id
     * @return AddressBook 地址对象
     */
    @Override
    public AddressBook getAddressBookById(Long id) {
        return baseMapper.selectById(id);
    }



    /**
     * 更新地址数据
     * @param addressBook AddressBook 地址数据对象
     * @return boolean true or false
     */
    @Override
    public boolean updateAddressBook(AddressBook addressBook) {
        return baseMapper.updateById(addressBook)>0?true:false;
    }


    /**
     * 根据主键id删除地址数据
     * @param id Long 地址主键
     * @return boolean true or false
     */
    @Override
    public boolean deleteByIds(Long id) {
        return baseMapper.deleteById(id)>0?true:false;
    }


    /**
     * 根据用户id获取默认地址
     * @param uid Long 用户id
     * @return AddressBook 地址实体对象
     */
    @Override
    public AddressBook getDefaultAddressBookByUserId(Long uid) {
        return baseMapper.selectOne(new LambdaQueryWrapper<AddressBook>().eq(AddressBook::getUserId,uid).eq(AddressBook::getIsDefault,1));
    }
}




